using Aliyun.OSS;
using System;
using System.IO;
using UnityEngine;

public class AliyunOSSTool
{
    public static string localRoot = "Data/";
    public static void UploadFile(string path)
    {
        UploadFiles(new string[] { path });
    }
    public static void UploadDirectory(string path)
    {
        if (Directory.Exists(localRoot + path))
        {
            string[] paths = Directory.GetFiles(localRoot + path, "*", SearchOption.AllDirectories);
            for (int i = 0; i < paths.Length; i++)
            {
                paths[i] = paths[i].Remove(0, 5).Replace('\\', '/');
            }
            UploadFiles(paths);
        }
        else
        {
            Debug.LogWarning(localRoot + path + "目录不存在");
        }
    }
    public static void UploadFiles(string[] paths)
    {
        // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        const string accessKeyId = "LTAI5tNCQd1kfEN9pEKUK5CZ";
        const string accessKeySecret = "gXBbqf1zr4C1uOPeL2kVsqFbJrVfrw";
        // 填写Bucket所在地域对应的Endpoint。以华东1（杭州）为例，Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
        const string endpoint = "https://oss-cn-shanghai.aliyuncs.com";

        // 创建OssClient实例。
        var client = new OssClient(endpoint, accessKeyId, accessKeySecret);

        foreach (var path in paths)
        {
            if (File.Exists(localRoot + path))
            {
                try
                {
                    byte[] binaryData = File.ReadAllBytes(localRoot + path);
                    MemoryStream requestContent = new MemoryStream(binaryData);
                    // 上传文件。
                    client.PutObject("htdw", "CQ/" + path, requestContent);
                    Debug.Log("上传文件成功 path=" + path);
                }
                catch (Exception ex)
                {
                    Debug.Log("上传文件失败 path=" + path + " " + ex.Message);
                }
            }
            else
            {
                Debug.LogWarning(localRoot + path + "文件不存在");
            }
        }
    }
}
